using System;


namespace OF.Security.Utils
{
	public static class DataConverter
	{
		public static TResult ChangeType<TSource, TResult>(TSource srcValue)
		{
			return (TResult)((object)DataConverter.ChangeType<TSource>(srcValue, typeof(TResult)));
		}

		public static object ChangeType<TSource>(TSource srcValue, Type targetType)
		{
			ExceptionHelper.FalseThrow<ArgumentNullException>(targetType != null, "targetType", new object[0]);
			bool flag = false;
			object result = null;
			Type type = typeof(TSource);
			if (type == typeof(object) && srcValue != null)
			{
				type = srcValue.GetType();
			}
			if (targetType.IsEnum)
			{
				if (type == typeof(string) || type == typeof(int))
				{
					result = Enum.Parse(targetType, srcValue.ToString());
					flag = true;
				}
			}
			else if (targetType == typeof(TimeSpan))
			{
				if (type == typeof(TimeSpan))
				{
					result = srcValue;
				}
				else
				{
					result = TimeSpan.FromSeconds((double)Convert.ChangeType(srcValue, typeof(double)));
				}
				flag = true;
			}
			else if (targetType == typeof(bool) && type == typeof(string))
			{
				result = DataConverter.StringToBool(srcValue.ToString(), out flag);
			}
			if (!flag)
			{
				if (targetType != typeof(object) && targetType.IsAssignableFrom(type))
				{
					result = srcValue;
				}
				else
				{
					result = Convert.ChangeType(srcValue, targetType);
				}
			}
			return result;
		}

		private static bool StringToBool(string srcValue, out bool dealed)
		{
			bool result = false;
			dealed = false;
			if (srcValue.Length > 0)
			{
				if (srcValue.Length == 1)
				{
					result = (string.Compare(srcValue, "0") != 0 && string.Compare(srcValue, "n", true) != 0);
					dealed = true;
				}
			}
			return result;
		}

	}
}
